package demo3;

public class Text1 {
    public static void main(String[] args) throws InterruptedException {
        Counter1 counter1 = new Counter1();
        Thread t1 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                counter1.increase();
            }
        });
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 5000; i++) {
                counter1.increase();
            }
        });

        t1.start();
        t2.start();

        t1.join();;
        t2.join();

        System.out.println("count = " + counter1.count);
    }
}

class Counter1 {
    public static int count = 0;
    /**
     * 累加方法
     */
    public synchronized void increase () {
        increase1();
    }

    private synchronized void increase1() {
        increase2();
    }

    private void increase2() {
        synchronized (this) {
            count++;
        }
    }
}